package com.kool.kadmin.system.service;

import com.kool.kadmin.common.bean.PageListResult;
import com.kool.kadmin.common.context.ReqContext;
import com.kool.kadmin.common.util.PaginatorUtil;
import com.kool.kadmin.common.util.ResultUtil;
import com.kool.kadmin.system.bean.SysNotifyDo;
import com.kool.kadmin.system.constant.MsgTypeEnum;
import com.kool.kadmin.system.constant.NotifyStateEnum;
import com.kool.kadmin.system.dao.SysNotifyDao;
import com.kool.kadmin.system.request.GetMyNotifyPageReq;
import com.kool.kadmin.system.request.SendMsgReq;
import com.kool.kadmin.system.request.SetOneReadReq;
import com.kool.kadmin.system.vo.MessageVo;
import com.kool.kadmin.system.vo.NotifyVo;
import com.kool.kadmin.system.ws.WebSocketSession;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luyu
 */
@Service
@Slf4j
public class NotifyService {
    @Resource
    private SysNotifyDao notifyDao;

    /**
     * 分页查询我的通知消息列表
     *
     * @param request
     * @return
     */
    public PageListResult<NotifyVo> getMyNotifyPage(GetMyNotifyPageReq request) {
        int currentPage = request.getCurrentPage();
        int pageSize = request.getPageSize();

        SysNotifyDo condition = new SysNotifyDo();
        condition.setReceiverId(ReqContext.getUserId());
        condition.setState(request.getState());

        int total = notifyDao.countTotal(condition);

        int offset = PaginatorUtil.calculateOffset(currentPage, pageSize);

        List<NotifyVo> list = notifyDao.getNotifyList(condition, offset, pageSize);

        return ResultUtil.success(list, currentPage, pageSize, total);
    }

    /**
     * 设置通知消息状态为已读
     *
     * @param request
     */
    public void setOneRead(SetOneReadReq request) {
        SysNotifyDo notifyDo = new SysNotifyDo();
        notifyDo.setId(request.getId());
        notifyDo.setState(NotifyStateEnum.READ.getCode());
        notifyDao.updateById(notifyDo);
    }

    /**
     * 设置所有通知消息状态为已读
     */
    public void setAllRead() {
        long userId = ReqContext.getUserId();
        notifyDao.updateAllRead(userId);
    }

    /**
     * 发送消息
     *
     * @param request
     */
    public void sendMsg(SendMsgReq request) {
        MessageVo messageVo = new MessageVo();
        messageVo.setSenderId(request.getSenderId())
                .setReceiverId(request.getReceiverId())
                .setContent(request.getContent())
                .setMsgType(request.getMsgType())
                .setServiceName(request.getServiceName());

        if (MsgTypeEnum.getByCode(request.getMsgType()) != null) {
            notifyInfo(messageVo);
        }else{
            WebSocketSession.sendInfo(JSON.toJSONString(messageVo), messageVo.getReceiverId());
        }
    }

    /**
     * 接收服务方推送的通知消息
     *
     * @param messageVo
     */
    public void notifyInfo(MessageVo messageVo) {
        //插入消息，状态未读
        SysNotifyDo sysNotifyDo = new SysNotifyDo();
        sysNotifyDo.setSenderId(messageVo.getSenderId())
                .setReceiverId(messageVo.getReceiverId())
                .setMsgType(messageVo.getMsgType())
                .setContent(messageVo.getContent())
                .setServiceName(messageVo.getServiceName())
                .setState(NotifyStateEnum.UNREAD.getCode());
        notifyDao.insert(sysNotifyDo);

        messageVo.setId(sysNotifyDo.getId());
        //发送消息
        WebSocketSession.sendInfo(JSON.toJSONString(messageVo), messageVo.getReceiverId());
    }
}
